package com.mjh.partners.controller;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mjh.partners.pojo.Book;
import com.mjh.partners.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONArray;
import sun.misc.BASE64Decoder;


@RequestMapping("/BookController")
@Controller
public class BookController {
  @Autowired
  private BookService bookService;
  @ResponseBody
  @RequestMapping(value="getBookByUid",produces = {"application/json;charset=UTF-8"})
  public String getBookByUid(int userid,HttpServletRequest request,HttpServletResponse response) throws  Exception{
    List<Book> Lists=bookService.getBookByUid(userid);
    response.setContentType("text/html;charset=utf-8");
    JSONArray json = JSONArray.fromObject(Lists);  
    String result=json.toString();
    return result;
  }
  
  public String upload(String base64Ex,Date dt,int userid) {
    String reslut = null;
    BASE64Decoder decoder = new BASE64Decoder();  
    try   {
        byte[] b = decoder.decodeBuffer(base64Ex);  
        for(int i=0;i<b.length;++i)  
        {  
            if(b[i]<0)  
            {
                b[i]+=256;  
            } 
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String fileName = "Bimg_"+sdf.format(dt)+String.valueOf(userid)+".png";
        String imgFilePath = "C:\\picture132\\recommendation\\"+fileName;
        reslut= "http://localhost:8080/Partners/picture132/recommendation/"+fileName;
        
        OutputStream out = new FileOutputStream(imgFilePath);      
        out.write(b);  
        out.flush();  
        out.close();
        return reslut;
    }catch (Exception e) {
      // TODO: handle exception
      return "error";
    }
    
  }
  @ResponseBody
  @RequestMapping("addBook")
  public String addBook(int userid,String base64,String bName,String introduce){
    Date dt = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String bImage=null;;
    Book book=new Book();
    String nowString=sdf.format(dt);
    book.setbName(bName);
    book.setIntroduce(introduce);
    if(base64.length()>5) {
      bImage=upload(base64,dt,userid);
      book.setbImage(bImage);
    }
    book.setUserid(userid);
    book.setCreatetime(nowString);
    boolean flag=bookService.addBook(book);
    if(flag) {
      return "success";
    }else {
      return "erorr";
    }
    }
  @ResponseBody
  @RequestMapping("deleteB")
  public String deleteB(int bid) {
    boolean flag=bookService.deleteBook(bid);
    if(flag) {
      return "success";
    }else {
      return "error";
    }
  }
  
}
